﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Newtonsoft.Json;

namespace WebApplication2.SignalR
{
    [HubName("UserHub")]
    public class UserHub : Hub
    {
        public class CustomUserIdProvider : IUserIdProvider
        {
            public string GetUserId(IRequest request)
            {
                var userId = "DD123";
                
                return userId.ToString();
            }
        }
        public void Hello()
        {

            Clients.All.hello();
        }
        public static List<User> users = new List<User>();

        //发送消息  
        public void SendMessage(string connectionId, string message)
        {
            Clients.All.hello();
            //var user = users.Where(s => s.ConnectionID == connectionId).FirstOrDefault();
            var user = users.Where(s => s.Name == connectionId).FirstOrDefault();
            if (user != null)
            {
                Clients.Client(user.ConnectionID).addMessage(message + "" + DateTime.Now, Context.ConnectionId);
                //给自己发送，把用户的ID传给自己  
                //Clients.Client(Context.ConnectionId).addMessage(message + "" + DateTime.Now, connectionId);
            }
            else
            {
                Clients.Client(Context.ConnectionId).showMessage("该用户已离线");
            }
        }
        [HubMethodName("getName")]
        public void GetName(string name)
        {
            //查询用户  
            var user = users.SingleOrDefault(u => u.ConnectionID == Context.ConnectionId);
            if (user != null)
            {
                Clients.User(name);
                user.Name = name;
                Clients.Client(Context.ConnectionId).showId(Context.ConnectionId);
            }
            GetUsers();
        }

        /// <summary>  
        /// 重写连接事件  
        /// </summary>  
        /// <returns></returns>  
        public override Task OnConnected()
        {
            //查询用户  
            var user = users.SingleOrDefault(w => w.ConnectionID == Context.ConnectionId);
            //判断用户是否存在，否则添加集合  
            if (user == null)
            {
                user = new User("", Context.ConnectionId);
                users.Add(user);
            }
            return base.OnConnected();
        }
        public override Task OnDisconnected(bool stopCalled)
        {
            var user = users.FirstOrDefault(p => p.ConnectionID == Context.ConnectionId);
            //判断用户是否存在，存在则删除  
            if (user != null)
            {
                //删除用户  
                users.Remove(user);
            }
            GetUsers();//获取所有用户的列表  
            return base.OnDisconnected(stopCalled);
        }
        //获取所有用户在线列表  
        private void GetUsers()
        {
            var list = users.Select(s => new { s.Name, s.ConnectionID }).ToList();
            string jsonList = JsonConvert.SerializeObject(list);
            Clients.All.getUsers(jsonList);
        }
    }
    public class User
    {
        [Key]
        public string ConnectionID { get; set; }
        public string Name { get; set; }
        public User(string name, string connectionId)
        {
            this.Name = name;
            this.ConnectionID = connectionId;
        }
    }
}